import axios from 'axios'            //引入axios
import qs from 'qs'  
import router from '../router/index'        //引入路由
import { Toast } from 'vant' //引入提示层
 
// 默认配置
axios.defaults.withCredentials = false
// axios.defaults.timeout = 50000
axios.defaults.headers = { 'X-Requested-With': 'XMLHttpRequest' }
axios.defaults.baseURL = process.VUE_APP_URL


// 请求拦截器
axios.interceptors.request.use(
    config => {
        Toast.loading({
            message: '加载中...',
            forbidClick: true,
          });
        if (config.method === 'get') {
        }
        return config
    },
    err => {
        Toast.fail('请求错误')
        return Promise.reject(err)
    }
)
// 响应拦截器
axios.interceptors.response.use(
    response => {
        Toast.clear();
        // 请求成功的时候更新token
        const token = response.headers["authorization"];  // 需要服务器设置 'Access-Control-Expose-Headers' : 'Authorization',
        if (token) {
            localStorage.setItem('token', token);
        }

        return response
    },
    error => {
        if (error.response && error.response.status === 401) {
            router.push({
                path: '/login',
                query: {
                    url: encodeURIComponent(location.hash.substr(1))
                }
            })
        } else {
            Toast({
                message: '请重新进入！' + error,
                position: 'top',
                duration: 2000
            })
            console.log(error.response)
        }
        
        return Promise.reject(error)
    }
)
// 错误处理
function checkStatus(response) {
    return new Promise((resolve, reject) => {
        if (response.status === 200) {
            resolve(response.data)
        } else {
            reject(response.data)
        }
    })
}

export default {
    get(url, params) {
        return axios({
            method: 'get',
            url,
            params,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'Authorization': `${localStorage.getItem('token')}`
            }
        }).then(checkStatus)
    },
    post(url, data) {
        return axios({
            method: 'post',
            url,
            data: qs.stringify(data),
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'Authorization': `${localStorage.getItem('token')}`
            }
        }).then(checkStatus)
    },
    put(url, data) {
        return axios({
            method: 'put',
            url,
            data: qs.stringify(data)
        }).then(checkStatus)
    },
    putParams(url, params) {
        return axios({
            method: 'put',
            url,
            params
        }).then(checkStatus)
    },
    putForm(url, data) {
        return axios({
            method: 'put',
            url,
            data: qs.stringify(data),
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        })
    },
    delete(url, params) {
        return axios({
            method: 'delete',
            url,
            params
        }).then(checkStatus)
    }

}